---
title: "Middleware"
description: "Middleware API Documentation"
icon: "code"
github: "https://github.com/mcp-use/mcp-use/blob/main/libraries/python/mcp_use/middleware/middleware.py"
---

import {RandomGradientBackground} from "/snippets/gradient.jsx"

<Callout type="info" title="Source Code">
View the source code for this module on GitHub: <a href='https://github.com/mcp-use/mcp-use/blob/main/libraries/python/mcp_use/middleware/middleware.py' target='_blank' rel='noopener noreferrer'>https://github.com/mcp-use/mcp-use/blob/main/libraries/python/mcp_use/middleware/middleware.py</a>
</Callout>

## CallbackClientSession

<div>
<RandomGradientBackground className="rounded-lg p-4 w-full h-full rounded-full">
<div className="text-black">
<div className="text-black font-bold text-xl mb-2 mt-8"><code className="!text-black">class</code> CallbackClientSession</div>

</div>
</RandomGradientBackground>
```python
from mcp_use.middleware.middleware import CallbackClientSession
```

<Card type="info">
### `method` __init__

**Parameters**
><ParamField body="client_session" type="mcp.client.session.ClientSession" required="True" >   MCP session instance </ParamField>
><ParamField body="connector_id" type="str" required="True" >   Connector instance </ParamField>
><ParamField body="middleware_manager" type="mcp_use.client.middleware.middleware.MiddlewareManager" required="True" >   Middleware instance </ParamField>

**Signature**
```python wrap
def __init__(client_session: mcp.client.session.ClientSession, connector_id: str, middleware_manager: mcp_use.client.middleware.middleware.MiddlewareManager):
```

</Card>
</div>

## MCPResponseContext

<div>
<RandomGradientBackground className="rounded-lg p-4 w-full h-full rounded-full">
<div className="text-black">
<div className="text-black font-bold text-xl mb-2 mt-8"><code className="!text-black">class</code> MCPResponseContext</div>

</div>
</RandomGradientBackground>
```python
from mcp_use.middleware.middleware import MCPResponseContext
```

<Card type="info">
### `method` __init__

**Parameters**
><ParamField body="request_id" type="str" required="True" >   String value </ParamField>
><ParamField body="result" type="Any" required="True" >   Parameter value </ParamField>
><ParamField body="error" type="Exception | None" required="True" >   Parameter value </ParamField>
><ParamField body="duration" type="float" required="True" >   Parameter value </ParamField>
><ParamField body="metadata" type="dict[str, Any]" required="True" >   Dictionary of key-value pairs </ParamField>
><ParamField body="jsonrpc_response" type="mcp.types.JSONRPCResponse | None" default="None" >   Parameter value </ParamField>

**Signature**
```python wrap
def __init__(request_id: str, result: Any, error: Exception | None, duration: float, metadata: dict[str, Any], jsonrpc_response: mcp.types.JSONRPCResponse | None = None):
```

</Card>
</div>

## Middleware

<div>
<RandomGradientBackground className="rounded-lg p-4 w-full h-full rounded-full">
<div className="text-black">
<div className="text-black font-bold text-xl mb-2 mt-8"><code className="!text-black">class</code> Middleware</div>

</div>
</RandomGradientBackground>
```python
from mcp_use.middleware.middleware import Middleware
```

</div>

## MiddlewareContext

<div>
<RandomGradientBackground className="rounded-lg p-4 w-full h-full rounded-full">
<div className="text-black">
<div className="text-black font-bold text-xl mb-2 mt-8"><code className="!text-black">class</code> MiddlewareContext</div>

</div>
</RandomGradientBackground>
```python
from mcp_use.middleware.middleware import MiddlewareContext
```

<Card type="info">
### `method` __init__

**Parameters**
><ParamField body="id" type="str" required="True" >   String value </ParamField>
><ParamField body="method" type="str" required="True" >   String value </ParamField>
><ParamField body="params" type="mcp_use.client.middleware.middleware.T" required="True" >   Parameter value </ParamField>
><ParamField body="connection_id" type="str" required="True" >   String value </ParamField>
><ParamField body="timestamp" type="float" required="True" >   Parameter value </ParamField>
><ParamField body="metadata" type="dict[str, Any]" default="<factory>" >   Dictionary of key-value pairs </ParamField>

**Signature**
```python wrap
def __init__(id: str, method: str, params: mcp_use.client.middleware.middleware.T, connection_id: str, timestamp: float, metadata: dict[str, Any] = <factory>):
```

</Card>
</div>

## MiddlewareManager

<div>
<RandomGradientBackground className="rounded-lg p-4 w-full h-full rounded-full">
<div className="text-black">
<div className="text-black font-bold text-xl mb-2 mt-8"><code className="!text-black">class</code> MiddlewareManager</div>

</div>
</RandomGradientBackground>
```python
from mcp_use.middleware.middleware import MiddlewareManager
```

<Card type="info">
### `method` __init__

**Signature**
```python wrap
def __init__():
```

</Card>
</div>

## NextFunctionT

<div>
<RandomGradientBackground className="rounded-lg p-4 w-full h-full rounded-full">
<div className="text-black">
<div className="text-black font-bold text-xl mb-2 mt-8"><code className="!text-black">class</code> NextFunctionT</div>

</div>
</RandomGradientBackground>
```python
from mcp_use.middleware.middleware import NextFunctionT
```

<Card type="info">
### `method` __init__

**Parameters**
><ParamField body="args" required="True" >   Parameter value </ParamField>
><ParamField body="kwargs" required="True" >   Parameter value </ParamField>

**Signature**
```python wrap
def __init__(args, kwargs):
```

</Card>
</div>
